package com.yunxi.module.infra.controller.admin.file;

import com.yunxi.framework.common.pojo.CommonResult;
import com.yunxi.framework.common.pojo.PageResult;
import com.yunxi.framework.common.util.object.BeanUtils;
import com.yunxi.module.infra.model.file.FileConfigDO;
import com.yunxi.module.infra.service.file.FileConfigService;
import com.yunxi.module.infra.vo.admin.file.config.FileConfigPageReqVO;
import com.yunxi.module.infra.vo.admin.file.config.FileConfigRespVO;
import com.yunxi.module.infra.vo.admin.file.config.FileConfigSaveReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 管理后台 - 文件配置
 * </p>
 *
 * @author lidy
 * @since 2024-05-25
 */
@Schema(name = "管理后台 - 文件配置")
@RestController
@RequestMapping("/infra/fileConfig")
@Validated
public class FileConfigController {

    @Resource
    private FileConfigService fileConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建文件配置")
    @PreAuthorize("@ss.hasPermission('infra:file-config:create')")
    public CommonResult<Long> createFileConfig(@Valid @RequestBody FileConfigSaveReqVO createReqVO) {
        return CommonResult.success(fileConfigService.createFileConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文件配置")
    @PreAuthorize("@ss.hasPermission('infra:file-config:update')")
    public CommonResult<Boolean> updateFileConfig(@Valid @RequestBody FileConfigSaveReqVO updateReqVO) {
        fileConfigService.updateFileConfig(updateReqVO);
        return CommonResult.success(true);
    }

    @PutMapping("/updateMaster")
    @Operation(summary = "更新文件配置为主配置")
    @PreAuthorize("@ss.hasPermission('infra:file-config:update')")
    public CommonResult<Boolean> updateFileConfigMaster(@RequestParam("configId") Long configId) {
        fileConfigService.updateFileConfigMaster(configId);
        return CommonResult.success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文件配置")
    @Parameter(name = "configId", description = "配置编号", required = true)
    @PreAuthorize("@ss.hasPermission('infra:file-config:delete')")
    public CommonResult<Boolean> deleteFileConfig(@RequestParam("configId") Long configId) {
        fileConfigService.deleteFileConfig(configId);
        return CommonResult.success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文件配置")
    @Parameter(name = "configId", description = "配置编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('infra:file-config:query')")
    public CommonResult<FileConfigRespVO> getFileConfig(@RequestParam("configId") Long configId) {
        FileConfigDO fileConfigDO = fileConfigService.getFileConfig(configId);
        return CommonResult.success(BeanUtils.toBean(fileConfigDO, FileConfigRespVO.class));
    }

    @GetMapping("/test")
    @Operation(summary = "测试文件配置是否正确")
    @PreAuthorize("@ss.hasPermission('infra:file-config:query')")
    public CommonResult<String> testFileConfig(@RequestParam("configId") Long configId) throws Exception {
        String url = fileConfigService.testFileConfig(configId);
        return CommonResult.success(url);
    }

    @GetMapping("/page")
    @Operation(summary = "获得文件配置分页列表")
    @PreAuthorize("@ss.hasPermission('infra:file-config:query')")
    public CommonResult<PageResult<FileConfigRespVO>> getFileConfigPage(@Valid FileConfigPageReqVO pageReqVO) {
        // 查询数据
        PageResult<FileConfigDO> pageResult = fileConfigService.getFileConfigPage(pageReqVO);
        return CommonResult.success(BeanUtils.toBean(pageResult, FileConfigRespVO.class));
    }

}
